package com.vcaresolution.logistic.service;

//import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.pojo.Codetable;
import com.vcaresolution.logistic.to.CodetableTO;
import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

@Service
public class CodetableServiceImpl extends BaseService implements CodetableService{

	@Override
	@Transactional
	public List<CodetableTO> getCodetable(String formdescription) {
		List<CodetableTO> codetableFormList = new ArrayList<CodetableTO>();
		
		if( !ApplicationConstants.CODETABLELS.isEmpty() ){
			List<Codetable> codetableList = getDaoFactory().getCodetableDAO().getCodetable(formdescription);
			
			
			
			if( codetableList != null && !codetableList.isEmpty() ){
				CodetableTO codetableForm = null;
				for(Codetable codetable: codetableList){
					codetableForm = new CodetableTO();
					
					BeanUtils.copyProperties(codetable, codetableForm);
					codetableFormList.add(codetableForm);
				}
			}	
		}
		else{
			codetableFormList = ApplicationConstants.CODETABLELS.get(formdescription);
		}
		
		return codetableFormList;
	}

	@Override
	@Transactional
	public DataHolderTO add(String formvalue, String formdescription) {
		Codetable codetable = new Codetable();
		codetable.setFormvalue(formvalue);
		codetable.setFormdescription(formdescription);
		
		System.out.println("Calling add now");
		getDaoFactory().getCodetableDAO().add(codetable);
		
		DataHolderTO dataHolderTO = new DataHolderTO();
		dataHolderTO.setFieldId(codetable.getCodetableid());
		dataHolderTO.setFieldname(formvalue);
		System.out.println("Data holder TO is "+dataHolderTO);
		
		return dataHolderTO;
	}

	/* (non-Javadoc)
	 * This method is used to load CodeTable when the server startup. This can be used to refresh the code table if needed
	 * 
	 * @see com.vcaresolution.logistic.service.CodetableService#loadCodeTables()
	 */
	@Override
	@Transactional
	public void loadCodeTables() {
		List<Codetable> codeTables = getDaoFactory().getUtilityDAO().findByAll(Codetable.class);
		
		Map<String,List<CodetableTO>> codeTableMap = new HashMap<String, List<CodetableTO>>();
		Map<Integer, String> codeTableValuesMap = new HashMap<Integer, String>();
		for(Codetable codeTable : codeTables){
			List<CodetableTO> list = codeTableMap.get(codeTable.getFormdescription());
			
			if( list == null ){
				list = new ArrayList<CodetableTO>();
			}
			
			CodetableTO codetableTO = new CodetableTO();
			BeanUtils.copyProperties(codeTable, codetableTO);
			
			list.add(codetableTO);
			
			codeTableMap.put(codeTable.getFormdescription(), list);
			codeTableValuesMap.put(codeTable.getCodetableid(), codeTable.getFormvalue());
		}
		
		ApplicationConstants.CODETABLELS.clear();
		ApplicationConstants.CODETABLELS.putAll(codeTableMap);
		
		ApplicationConstants.CODETABLELVALUES.clear();
		ApplicationConstants.CODETABLELVALUES.putAll(codeTableValuesMap);
	}
}